import '../models/user.dart';
import '../config/app_config.dart';
import '../utils/logger.dart'; // 导入日志工具类

class ApiService {
  final String baseUrl = AppConfig.apiBaseUrl;
  final Logger _logger = Logger(); // 创建日志实例
  
  // 模拟登录API
  Future<User> login(String username, String password) async {
    _logger.info('开始登录请求: 用户名=$username');
    final startTime = DateTime.now().millisecondsSinceEpoch;
    
    try {
      // 在实际应用中，这里应该是一个真实的API调用
      // 这里我们模拟一个成功的登录响应
      await Future.delayed(const Duration(seconds: 2)); // 模拟网络延迟
      
      if (username == 'test' && password == '123456') {
        // 模拟成功登录返回用户信息
        final user = User(
          nick: '测试用户',
          avatar: 'https://via.placeholder.com/150',
          token: 'mock_token_${DateTime.now().millisecondsSinceEpoch}',
          sex: 'male',
          birthday: DateTime(1990, 1, 1),
        );
        
        final endTime = DateTime.now().millisecondsSinceEpoch;
        _logger.api('POST', '$baseUrl/login', 
          statusCode: 200, 
          duration: endTime - startTime
        );
        
        return user;
      } else {
        // 模拟登录失败
        final endTime = DateTime.now().millisecondsSinceEpoch;
        _logger.api('POST', '$baseUrl/login', 
          statusCode: 401, 
          duration: endTime - startTime,
          error: '用户名或密码错误'
        );
        
        throw Exception('用户名或密码错误');
      }
    } catch (e) {
      final endTime = DateTime.now().millisecondsSinceEpoch;
      _logger.error('登录失败', exception: e);
      _logger.api('POST', '$baseUrl/login', 
        statusCode: 500, 
        duration: endTime - startTime,
        error: e.toString()
      );
      rethrow;
    }
  }
  
  // 使用token获取用户信息
  Future<User> getUserInfo(String token) async {
    _logger.info('获取用户信息: token=${token.substring(0, 10)}...');
    final startTime = DateTime.now().millisecondsSinceEpoch;
    
    try {
      // 在实际应用中，这里应该是一个真实的API调用
      // 这里我们模拟一个成功的响应
      await Future.delayed(const Duration(seconds: 1)); // 模拟网络延迟
      
      // 模拟返回用户信息
      final user = User(
        nick: '测试用户',
        avatar: 'https://via.placeholder.com/150',
        token: token,
        sex: 'male',
        birthday: DateTime(1990, 1, 1),
      );
      
      final endTime = DateTime.now().millisecondsSinceEpoch;
      _logger.api('GET', '$baseUrl/user/info', 
        statusCode: 200, 
        duration: endTime - startTime
      );
      
      return user;
    } catch (e) {
      final endTime = DateTime.now().millisecondsSinceEpoch;
      _logger.error('获取用户信息失败', exception: e);
      _logger.api('GET', '$baseUrl/user/info', 
        statusCode: 500, 
        duration: endTime - startTime,
        error: e.toString()
      );
      rethrow;
    }
  }
  
  // 登出
  Future<void> logout(String token) async {
    _logger.info('用户登出');
    final startTime = DateTime.now().millisecondsSinceEpoch;
    
    try {
      // 在实际应用中，这里应该是一个真实的API调用
      await Future.delayed(const Duration(seconds: 1)); // 模拟网络延迟
      // 模拟成功登出
      
      final endTime = DateTime.now().millisecondsSinceEpoch;
      _logger.api('POST', '$baseUrl/logout', 
        statusCode: 200, 
        duration: endTime - startTime
      );
      
      return;
    } catch (e) {
      final endTime = DateTime.now().millisecondsSinceEpoch;
      _logger.error('登出失败', exception: e);
      _logger.api('POST', '$baseUrl/logout', 
        statusCode: 500, 
        duration: endTime - startTime,
        error: e.toString()
      );
      rethrow;
    }
  }
}
